<?php

namespace app\erp\model;

use think\Model;
use think\Cache;
use traits\model\SoftDelete;

class PayLogdb extends Model {
	use SoftDelete;
	protected $pk = 'pay_log_id';
	protected $name = 'pay_log';
	protected $type = [ 
			'create_time' => 'timestamp:Y-m-d H:i',
			'audit_time' => 'timestamp:Y-m-d H:i' 
	];
	protected $autoWriteTimestamp = true;
	public $param = array ();
	// 自动完成
	protected $insert = [ 
			'partner_id' => PARTNER_ID,
			'site_id' 
	];
	protected function setSiteIdAttr($value) {
		if (! $value) {
			return SITE_ID;
		}
		if ($value == 0) {
			return null;
		}
		return $value;
	}
	protected function cache_name($name) {
		$new_arr = $this->param;
		$new_arr ['partner_id'] = PARTNER_ID;
		if (! isset ( $new_arr ['site_id'] )) {
			$new_arr ['site_id'] = SITE_ID;
		}
		ksort ( $new_arr );
		$param_str = http_build_query ( $new_arr );
		return md5 ( $name . $param_str );
	}
	public function scopeAuditState($query, $audit_state) {
		if (is_array ( $audit_state )) {
			$query->where ( 'audit_state', 'in', $audit_state );
		} else {
			$query->where ( 'audit_state', $audit_state );
		}
	}
	public function scopePayOption($query, $value) {
		$query->where ( 'pay_option', $value );
	}
	public function scopePayLogId($query, $pay_log_id) {
		$query->where ( 'pay_log_id', $pay_log_id );
	}
	public function scopeOrderNo($query, $order_no) {
		if (is_array ( $order_no )) {
			$temp = $order_no;
		} else {
			$temp = explode ( ',', $order_no );
		}
		if (count ( $temp ) == 1) {
			$query->where ( 'pay_order_no', 'like', '%' . $order_no . '%' );
		} else {
			$query->where ( '', 'exp', 'find_in_set(`pay_order_no`,"' . $temp . '")' );
		}
	}
	public function scopeUsername($query, $username) {
		$memberdb = new Memberdb ();
		$memberdb->param ['username_like'] = $username;
		$memberdb->param ['no_need_member_handle'] = 'yes';
		$memberdb->param ['_o'] = 'member_id';
		$memberdb->param ['_b'] = 'asc';
		$memberdb->param ['page'] = 1;
		$memberdb->param ['limit'] = 1000;
		$get_member = $memberdb->ajaxList ();
		$member_id_arr = array ();
		foreach ( $get_member as $key => $val ) {
			array_push ( $member_id_arr, $val ['member_id'] );
		}
		if (count ( $member_id_arr ) > 0) {
			$query->where ( 'member_id', 'IN', $member_id_arr );
		} else {
			$query->where ( 'member_id', - 1 );
		}
	}
	public function scopeApplyAdminId($query, $apply_admin_id) {
		$query->where ( 'apply_admin_id', $apply_admin_id );
	}
	// 基础查询
	public function base($query) {
		$query->where ( 'partner_id', PARTNER_ID );
		$field = $this->getDeleteTimeField ( true );
		if ($field) {
			if (isset ( $this->param ['delete'] ) && $this->param ['delete'] == 'yes') {
				$this->onlyTrashed ();
			} else {
				$query->useSoftDelete ( $field );
			}
		}
		if (isset ( $this->param ['site_id'] )) {
			if ($this->param ['site_id'] == 0 || $this->param ['site_id'] == '') {
				$query->where ( 'site_id', null );
			} elseif ($this->param ['site_id'] != - 1) {
				$query->where ( 'site_id', $this->param ['site_id'] );
			}
		} else {
			if (! empty ( SITE_ID )) {
				$query->where ( 'site_id', SITE_ID );
			} else {
				$query->where ( 'site_id', null );
			}
		}
		if (isset ( $this->param ['audit_state'] ) && $this->param ['audit_state']) {
			$query->auditState ( $this->param ['audit_state'] );
		}
		if (isset ( $this->param ['pay_log_id'] ) && $this->param ['pay_log_id']) {
			$query->payLogId ( $this->param ['pay_log_id'] );
		}
		if (isset ( $this->param ['pay_bank'] ) && $this->param ['pay_bank']) {
			$query->payOption ( $this->param ['pay_bank'] );
		}
		if (isset ( $this->param ['pay_card'] ) && $this->param ['pay_card']) {
			$query->payOption ( $this->param ['pay_card'] );
		}
		if (isset ( $this->param ['pay_card_name'] ) && $this->param ['pay_card_name']) {
			$query->payOption ( $this->param ['pay_card_name'] );
		}
		if (isset ( $this->param ['pay_no'] ) && $this->param ['pay_no']) {
			$query->payOption ( $this->param ['pay_no'] );
		}
		if (isset ( $this->param ['order_no'] ) && $this->param ['order_no']) {
			$query->orderNo ( $this->param ['order_no'] );
		}
		if (isset ( $this->param ['username'] ) && $this->param ['username']) {
			$query->username ( $this->param ['username'] );
		}
		if (isset ( $this->param ['apply_admin_id'] ) && $this->param ['apply_admin_id']) {
			$query->applyAdminId ( $this->param ['apply_admin_id'] );
		}
	}
	public function ajaxCount() {
		$data = $this->cache ( $this->cache_name ( 'pay_log_count' ), 86400, 'pay_log' )->count ();
		return $data;
	}
	public function ajaxFind() {
		$data = $this->master ()->order ( 'pay_log_id desc' )->find ();
		if ($data) {
			if (! isset ( $this->param ['no_need_pay_log_handle'] )) {
				$data = $this->handleData ( $data->toArray () );
			}
			return $data;
		} else {
			return array ();
		}
	}
	public function ajaxList() {
		$data = $this->cache ( $this->cache_name ( 'pay_log_list' ), 86400, 'pay_log' )->page ( $this->param ['page'], $this->param ['limit'] )->order ( 'pay_log_id desc' )->select ();
		if ($data) {
			$data = $data->toArray ();
			if (! isset ( $this->param ['no_need_pay_log_handle'] )) {
				foreach ( $data as $key => $val ) {
					$data [$key] = $this->handleData ( $val );
				}
			}
			return $data;
		} else {
			return array ();
		}
	}
	public function ajaxSelect() {
		$return ['data'] = $this->ajaxList ();
		$return ['count'] = $this->ajaxCount ();
		return $return;
	}
	public function ajaxSave() {
		if (isset ( $this->param ['pay_log_id'] ) && $this->param ['pay_log_id']) {
			// 修改
			if (isset ( $this->param ['audit_state'] ) && $this->param ['audit_state'] == 3) {
				// 驳回
				$this->param ['audit_time'] = time ();
			}
			if (isset ( $this->param ['audit_state'] ) && $this->param ['audit_state'] == 2) {
				// 已到账
				$this->param ['audit_time'] = time ();
				$get_pay_log = $this->useGlobalScope ( false )->where ( 'pay_log_id', $this->param ['pay_log_id'] )->find ()->toArray ();
				$pay_option = json_decode ( $get_pay_log ['pay_option'], true );
				$pay_option ['pay_no'] = $this->param ['pay_no'];
				$pay_option ['pay_state'] = 2;
				$pay_option ['pay_time'] = time ();
				$this->param ['pay_option'] = json_encode ( $pay_option );
				// 处理订单收款状态
				$get_order_nos = explode ( ',', $get_pay_log ['pay_order_no'] );
				$orderdb = new Orderdb ();
				for($i = 0; $i < count ( $get_order_nos ); $i ++) {
					$orderdb->param = $pay_option;
					$orderdb->param ['event'] = 'make_pay';
					$orderdb->param ['order_no'] = $get_order_nos [$i];
					$orderdb->param ['cart_ids'] = explode ( ',', $get_pay_log ['pay_cart_id'] );
					$orderdb->editOrder ();
				}
			}
			$this->save ( $this->param, [ 
					'pay_log_id' => $this->param ['pay_log_id'] 
			] );
		} else {
			$pay_option = array ();
			if (isset ( $this->param ['pay_bank'] )) {
				$pay_option ['pay_bank'] = $this->param ['pay_bank'];
			}
			if (isset ( $this->param ['pay_card'] )) {
				$pay_option ['pay_card'] = $this->param ['pay_card'];
			}
			if (isset ( $this->param ['pay_card_name'] )) {
				$pay_option ['pay_card_name'] = $this->param ['pay_card_name'];
			}
			// $this->param['pay_option'] = json_encode($pay_option);
			// dump($this->param);
			$this->allowField ( true )->save ( $this->param );
		}
		Cache::clear ( 'pay_log' );
		return 'success';
	}
	// 软删除
	public function ajaxDelete() {
		if (isset ( $this->param ['pay_log_id'] )) {
			if (isset ( $this->param ['delete'] ) && $this->param ['delete'] == 'yes') {
				$this->destroy ( function ($query) {
					$query->where ( 'pay_log_id', 'IN', $this->param ['pay_log_id'] );
				}, true );
			} else {
				$this->destroy ( function ($query) {
					$query->where ( 'pay_log_id', 'IN', $this->param ['pay_log_id'] );
				} );
			}
			Cache::clear ( 'pay_log' );
			return 'success';
		}
	}
	// 数据处理
	protected function handleData($val) {
		$memberdb = new Memberdb ();
		foreach ( $val as $k => $v ) {
			$val [$k] = $v;
			switch ($k) {
				case 'audit_state' :
					if ($v == 1) {
						$val ['audit_state_text'] = '<font style=color:#FFB800>申请支付</font>';
					} elseif ($v == 2) {
						$val ['audit_state_text'] = '<font style=color:#5FB878>已收款</font>';
					} else {
						$val ['audit_state_text'] = '<font style=color:#FF5722>被驳回</font>';
					}
					break;
				case 'pay_cart_id' :
					if (isset ( $this->param ['need_cart_info'] ) && $this->param ['need_cart_info'] == 'yes') {
						$cartdb = new Cartdb ();
						$cartdb->param ['cart_id'] = explode ( ',', $v );
						$cartdb->param ['need_goods_title'] = 'yes';
						$get_cart = $cartdb->ajaxList ();
						$val ['cart_info'] = $get_cart ['list'];
					}
					break;
				case 'pay_money' :
					$val ['pay_money'] = number_format ( $v, 2, '.', '' );
					break;
				case 'member_id' :
					if (! isset ( $this->param ['no_need_pay_member_info'] )) {
						if ($v) {
							$memberdb->param ['member_id'] = $v;
							$get_member = $memberdb->ajaxFind ();
							if (isset ( $get_member ['member_id'] )) {
								$val ['username'] = $get_member ['realname'] ? $get_member ['realname'] : $get_member ['username'];
								$val ['phone'] = $get_member ['phone'] ? $get_member ['phone'] : '无';
							} else {
								$val ['username'] = '已被删除';
								$val ['phone'] = '已被删除';
							}
						}
					}
					break;
				case 'apply_admin_id' :
					if (! isset ( $this->param ['no_need_pay_member_info'] )) {
						if ($v) {
							$memberdb->param ['member_id'] = $v;
							$get_member = $memberdb->ajaxFind ();
							$val ['apply_admin'] = $get_member ['realname'] ? $get_member ['realname'] : $get_member ['username'];
						} else {
							if ($val ['audit_state'] == 2) {
								$val ['apply_admin'] = '<font style=color:#5FB878>在线支付</font>';
							} else {
								$val ['apply_admin'] = '';
							}
						}
					}
					break;
				case 'audit_admin_id' :
					if (! isset ( $this->param ['no_need_pay_member_info'] )) {
						if ($v) {
							$memberdb->param ['member_id'] = $v;
							$get_member = $memberdb->ajaxFind ();
							$val ['audit_admin'] = $get_member ['realname'] ? $get_member ['realname'] : $get_member ['username'];
						} else {
							if ($val ['audit_state'] == 2) {
								$val ['audit_admin'] = '<font style=color:#5FB878>在线支付</font>';
							} else {
								$val ['audit_admin'] = '';
							}
						}
					}
					break;
				case 'pay_option' :
					if ($v) {
						$temp = json_decode ( $v, true );
						$val = array_merge ( $val, $temp );
					}
					break;
				default :
					break;
			}
		}
		return $val;
	}
}
